package itheima2.Kaoshi;

import java.util.ArrayList;
import java.util.Scanner;
    class Student3 {

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("--------欢迎来到教务管理系统--------");
            ArrayList<Student>list= new ArrayList<>();
            end:
            while (true) {
                System.out.println("1 添加学生");
                System.out.println("2 删除学生");
                System.out.println("3 修改学生");
                System.out.println("4 查看学生");
                System.out.println("5 退出");
                System.out.println("请输入您的选择:");
                int num = sc.nextInt();
                switch (num) {
                    case 1:
    //                    System.out.println(" 添加学生信息");
                        addStudent(list);
                        break ;
                    case 2:
    //                    System.out.println(" 删除学生信息");
                        DelStudent(list);
                        break ;
                    case 3:
    //                    System.out.println(" 修改学生信息");
                        setStudent(list);
                        break ;
                    case 4:
    //                    System.out.println(" 查看学生信息");
                        chakanStudent(list);
                        break ;
                    case 5:
                        System.out.println(" 退出");
                         break end;
                    default:
                        System.out.println("呼哈");
                }
            }
        }

        public static void setStudent(ArrayList<Student> list) {
            Scanner s = new Scanner(System.in);
            System.out.println("请输入需要更改的学生学号");
            String sr = s.next();
            int Indxe = GetIndxe(list,sr);
            if (Indxe == -1) {
                System.out.println("输入有误");
            } else {
                System.out.println("请输入需要修改的学生姓名");
                String name=s.next();
                System.out.println("请输入需要修改的学生年龄");
                String old=s.next();
                System.out.println("请输入需要修改的学生班级");
                String banji=s.next();
              Student stu=new Student();
              list.set(Indxe,stu);
            }
        }
        public static int DelStudent(ArrayList<Student> list) {
             Scanner sc=new Scanner (System.in);
            System.out.println("请输入要删除的学号");
             String st=sc.next();
            int Indxe = GetIndxe(list, st);
            if (Indxe==-1){
                System.out.println("输入有误");
            }else {
                list.remove(Indxe);
                System.out.println("删除成功");
            }
            return Indxe;
        }

        public static void chakanStudent(ArrayList<Student> list) {
            if (list.size()==0){
                System.out.println("芜湖~~暂无信息~~");
                return;
            }
            System.out.println("姓名\t\t年龄\t\t班级\t\t学号");
            for (int i = 0; i < list.size(); i++) {
                Student st = list.get(i);
                System.out.println(st.getName()+"\t"+st.getOld()+"\t"+st.getBanji()+"\t"+st.getNumber());
            }
        }


        public static void addStudent(ArrayList<Student> list) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入学生姓名");
            String name=sc.next();
            System.out.println("请输入学生年龄");
            String old=sc.next();
            System.out.println("请输入学生班级");
            String banji=sc.next();
            System.out.println("请输入学生学号");
            String number=sc.next();
            int Indxe = GetIndxe(list,number);
            if (Indxe==-1){
                Student stu=new Student(name,old,banji,number);
                list.add(stu );
                System.out.println("添加成功");
            }else{
                System.out.println("该学号已存在,请检查后重新输入");
            }
        }
        public static int GetIndxe(ArrayList<Student>list, String number){
            int Indxe=-1;
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                String id=stu.getNumber();
                if (id.equals(number)){
                   Indxe=i;
                }
            }
            return Indxe;
        }
    }

